//
//  RECSearchViewController.m
//  CarFace
//
//  Created by dlios on 15/3/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RECSearchViewController.h"
#import "SearchOb.h"
#import "RECWebViewController.h"
#import "RECFastNewsController.h"

@interface RECSearchViewController () <UISearchBarDelegate, UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain)UIButton *button;
@property(nonatomic, retain)NSString *searchStr;

@property(nonatomic, retain)NSMutableArray *result_Arr;

@property(nonatomic, retain)UITableView *tableView;


@end


@implementation RECSearchViewController

- (void)dealloc
{
    [_tableView release];
//    [_searchStr release];
    [_result_Arr release];
    [_button release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor redColor];
    
    
//    self.result_Arr = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UISearchBar *searchbar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 22, self.view.frame.size.width , self.view.frame.size.height / 20)];
    searchbar.searchBarStyle = UISearchBarStyleMinimal;
    searchbar.placeholder = @"请输入搜索内容";
    searchbar.showsCancelButton = YES;
    searchbar.delegate = self;
    [self.view addSubview:searchbar];
//    [searchbar release];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 22 +  self.view.frame.size.height / 20, self.view.frame.size.width, self.view.frame.size.height - 22 - self.view.frame.size.height / 20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView ];
//    [_tableView release];
    
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"searchs"];
    
    self.result_Arr = [NSMutableArray array];

//    [_tableView addHeaderWithTarget:self action:@selector(addheader:)];
//    [_tableView addFooterWithTarget:self action:@selector(addfooter:)];
 
 
 
}

- (void)addheader:(UITableView *)tableview
{
    
}
- (void)addfooter:(UITableView *)tableview
{
    
}


- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSString *str = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.6/sou/search.ashx?q=%@&p=1&s=20&a=2&pm=2&v=4.6.1",searchText];
    self.searchStr = str;
    NSLog(@"%@", str);
    
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    searchBar.showsCancelButton = NO;
    NSLog(@"显示视图");
}

//- (void)buttonAction:(UIButton *)button
//{
//    NSLog(@"0.0.0.0.0.");
//}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}




- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"===");
    
    
    NSLog(@"===%ld", _result_Arr.count);
    NSLog(@"------------------------");
    
        AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
        manage.responseSerializer = [AFHTTPResponseSerializer serializer];
        [manage GET:[[self.searchStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
//           NSLog(@"%@", responseObject);
            NSError *error = nil;
            id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
            NSMutableArray *naturemodelArr = [[result objectForKey:@"result"] objectForKey:@"naturemodel"];
            NSMutableArray *ar = [NSMutableArray array];

            for (NSDictionary *dic in naturemodelArr) {
                SearchOb *sea = [SearchOb objectWithDictionary:dic];
                
                [ar addObject:sea];
                NSLog(@"%@", [sea idStr]);
                
            }
            self.result_Arr = ar;

            NSLog(@"--%ld", _result_Arr.count);
            [_tableView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"%@",error);
        }];
    


//    [_tableView reloadData];
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.frame.size.height / 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.result_Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"searchs"];
    cell.textLabel.text = [self.result_Arr[indexPath.row] title];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.numberOfLines = 3;
    [cell.textLabel sizeToFit];
//    cell.detailTextLabel.text = [self.result_Arr[indexPath.row] indexdetail];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    if (self.result_Arr.count != 0) {
         NSInteger media = [[self.result_Arr[indexPath.row] mediatype] integerValue];
        NSInteger str = [[self.result_Arr[indexPath.row] idStr] integerValue];
        
        NSString *str1 ;
        if (media == 1)
        {
            str1 = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.6/content/news/newscontent-n%ld-t0.json", str];
            
        }
        else if (media == 2)
        {
            str1 =[NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.6/content/news/shuokecontent-a2-pm2-v4.6.1-n%ld-nt0-lz0-p1-fs0-cw360.json", str];
        }
        else if (media == 3)
        {
            str1 =[NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.6/content/news/videopage-a2-pm2-v4.6.1-vid%ld-night0-showpage1-fs0-cw1080.json", str];
        }
        
        else if (media == 5 || media == 4)
        {
            str1 = [NSString stringWithFormat:@"http://forum.app.autohome.com.cn/autov4.6/forum/club/topiccontent-a2-pm2-v4.6.0-t%ld-o0-p1-s20-c1-nt0-fs0-sp0-al0-cw360.json", str];
        }
        else if (media == 6)
        {
            str1 =[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.6/cars/cardutu-a2-pm2-v4.6.1-ss2073-sp17815-imgid%ld-p1-s60-cid0-t0.html", str];
        }
        //   else if ([self.data.mediatype intValue] == 4) {
        //       str = [NSString stringWithFormat:@"http://forum.app.autohome.com.cn/autov4.6/forum/club/topiccontent-a2-pm2-v4.6.0-t39538716-o0-p1-s20-c1-nt0-fs0-sp0-al0-cw360.json", self.data.idStr];
        
        //   }
        else
        {
            str1 = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov4.6/content/News/fastnewscontent-n%ld-lastid0.json", str];
            RECFastNewsController *fastVC = [[RECFastNewsController alloc] init];
            fastVC.idStr = str1;
            [self presentViewController:fastVC animated:YES completion:^{
                
            }];
            [fastVC release];

            return;
        }
        
        RECWebViewController *recVC = [[RECWebViewController alloc] init];
        recVC.idStr = str1;
        [self presentViewController:recVC animated:YES completion:^{
        }];
        
    }
            
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
